package com.ly.lypopupwindowdemo.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.ldf.calendar.component.CalendarAttr;
import com.ldf.calendar.component.CalendarViewAdapter;
import com.ldf.calendar.interf.OnSelectDateListener;
import com.ldf.calendar.model.CalendarDate;
import com.ldf.calendar.view.Calendar;
import com.ldf.calendar.view.MonthPager;
import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.calendar.CustomDayView;
import com.ly.lypopupwindowdemo.util.Utils;

import java.util.ArrayList;
import java.util.HashMap;

public class TheThirdCalendar extends AppCompatActivity implements View.OnClickListener{

    TextView textViewYearDisplay;
    TextView textViewMonthDisplay;

    TextView backToday;
    TextView nextMonthBtn;
    TextView lastMonthBtn;




    private Context context;
    private CalendarViewAdapter calendarAdapter;
    private OnSelectDateListener onSelectDateListener;

    private ArrayList<Calendar> currentCalendars = new ArrayList<>();
    private int mCurrentPage = MonthPager.CURRENT_DAY_INDEX;
    private CalendarDate currentDate;

    CoordinatorLayout content;
    MonthPager monthPager;

    private boolean initiated = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_the_third_calendar);

        textViewYearDisplay = (TextView) findViewById(R.id.show_year_view);
        textViewMonthDisplay = (TextView) findViewById(R.id.show_month_view);
        backToday = (TextView) findViewById(R.id.back_today_button);
        nextMonthBtn = (TextView) findViewById(R.id.next_month);
        lastMonthBtn = (TextView) findViewById(R.id.last_month);
        context = this;
        content = (CoordinatorLayout) findViewById(R.id.content);
        monthPager = (MonthPager) findViewById(R.id.calendar_view);

        backToday.setOnClickListener(this);
        nextMonthBtn.setOnClickListener(this);
        lastMonthBtn.setOnClickListener(this);

        monthPager.setViewheight((int) Utils.dp2px2(context,270));

        initCalendarView();

    }

    private void initCalendarView() {

        initListener();
        CustomDayView customDayView = new CustomDayView(context, R.layout.custom_day);
        calendarAdapter = new CalendarViewAdapter(context,onSelectDateListener, CalendarAttr.CalendayType.MONTH,customDayView);
        initMarkData();
        initMonthPager();
    }

    private void initListener() {

        onSelectDateListener = new OnSelectDateListener() {
            @Override
            public void onSelectDate(CalendarDate date) {
                refreshClickDate(date);
            }

            @Override
            public void onSelectOtherMonth(int offset) {
                //偏移量 -1表示刷新成上一个月数据 ， 1表示刷新成下一个月数据
                monthPager.selectOtherMonth(offset);
            }
        };
    }

    private void refreshClickDate(CalendarDate date) {
        currentDate = date;
        String year_str = date.getYear()+"";
        String mon_str = date.getMonth()+"";
        String day_str = date.getDay()+"";
        textViewYearDisplay.setText(date.getYear() + "年");
        textViewMonthDisplay.setText(date.getMonth() + "");

        Log.e("liying","当前选中的日期是="+year_str+"/"+mon_str+"/"+day_str);
    }
    private void initMonthPager() {

        monthPager.setAdapter(calendarAdapter);
        monthPager.setCurrentItem(MonthPager.CURRENT_DAY_INDEX);
        monthPager.setPageTransformer(false,new ViewPager.PageTransformer(){

            @Override
            public void transformPage(View page, float position) {
                position = (float) Math.sqrt(1 - Math.abs(position));
                page.setAlpha(position);
            }
        });

        monthPager.addOnPageChangeListener(new MonthPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mCurrentPage = position;
                currentCalendars = calendarAdapter.getPagers();
                if (currentCalendars.get(position % currentCalendars.size()) instanceof Calendar) {
                    CalendarDate date = currentCalendars.get(position % currentCalendars.size()).getSeedDate();
                    currentDate = date;
                    textViewYearDisplay.setText(date.getYear() + "年");
                    textViewMonthDisplay.setText(date.getMonth() + "");
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void initMarkData() {

        HashMap<String, String> markData = new HashMap<>();
        markData.put("2017-8-9", "0");
        markData.put("2017-7-9", "0");
        markData.put("2017-6-9", "0");
        markData.put("2017-6-10", "0");
        calendarAdapter.setMarkData(markData);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        if (hasFocus && !initiated) {
            refreshMonthPager();
            initiated = true;
        }
    }
    public void onClickBackToDayBtn() {
        refreshMonthPager();
    }
    private void refreshMonthPager() {
        CalendarDate today = new CalendarDate();
        calendarAdapter.notifyDataChanged(today);
        textViewYearDisplay.setText(today.getYear() + "年");
        textViewMonthDisplay.setText(today.getMonth() + "");
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.back_today_button:
                onClickBackToDayBtn();
                break;
            case R.id.next_month:
                monthPager.setCurrentItem(monthPager.getCurrentPosition() + 1);
                break;
            case R.id.last_month:
                monthPager.setCurrentItem(monthPager.getCurrentPosition() - 1);
                break;
        }
    }
}
